package com.lancer.springboot.controller;

import com.lancer.springboot.Response;
import com.lancer.springboot.domain.Students;
import com.lancer.springboot.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

//默认返回json格式
@Api(tags = "学生管理")
@RestController
public class StudentController {

    @Resource
    StudentService studentService;

    //配置url映射。现在更多的也会直接用以Http Method直接关联的映射注解来定义
    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }

    @ApiOperation(value = "获取用户详细信息",response = Response.class)
    @GetMapping("/getStudent")
    public Response getStudent(@ApiParam(name = "学生id",required = true,example = "2") @RequestParam String id) {
        return Response.ok(studentService.findStudentById(Long.valueOf(id)));
    }

    @ApiOperation(value = "获取用户详细信息", notes = "路径参数ID",response = Response.class)
    @GetMapping("/getStudentPath/{id}")
    public Response getStudentPath(@PathVariable Long id) {
        return Response.ok(studentService.findStudentById(id));
    }

    @ApiOperation("查询所有学生")
    @GetMapping("/getAllStudents")
    public Response getAllStudents(){
        return Response.ok(studentService.getAllStudents());
    }

    @PostMapping("/student")
    public String addStudent(@RequestParam String name) {
        studentService.insertStudent(new Students(name));
        return "ok";
    }

    @PostMapping("/updateStudent")
    public String updateStudent(@RequestParam String id, @RequestParam String name) {
        studentService.updateStudent(new Students(Long.valueOf(id), name));
        return "ok";
    }

    @PostMapping("/deleteStudent")
    public String deleteStudent(@RequestParam String id) {
        studentService.deleteStudent(Long.valueOf(id));
        return "ok";
    }

}
